
/**
 * Copyright (c) 2008,中企动力华南研发中心
 * All rights reserved.
 */

package com.sitechasia.webx.components.xtable.view;

import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.bean.Table;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.HtmlView;
import org.extremecomponents.util.HtmlBuilder;

import com.sitechasia.webx.components.xtable.customertag.CeRowBuilder;
import com.sitechasia.webx.components.xtable.toolbar.CeToolBar;

/**
 * 逻辑、功能相关描述:主要是在原有的ectable中添加了一个表格foot，显示分页的内容
 *
 * @author 易贤建  编写日期：2008-4-29
 */
public class CeHtmlViewNotToolBar extends HtmlView {



	@Override
	protected void init(HtmlBuilder builder, TableModel model) {
		super.init(builder, model);
		setRowBuilder(new CeRowBuilder(builder,model));
	}

	@Override
	protected void beforeBodyInternal(TableModel model) {
		//显示表格表头--标题部分
		buildTableHead(model);
		getHtmlBuilder().div().styleClass("grid_div").close();
		//显示表格数据部分
		buildTableDataBody();
	}

	@Override
	public void body(TableModel model, Column column){

		if(column.isFirstColumn()){
			getRowBuilder().rowStart();

		}


		if("tdBodyContrl".equalsIgnoreCase(column.getStyleClass())){

			String cellDisplay = column.getCellDisplay();

			cellDisplay=cellDisplay.substring(cellDisplay.indexOf(">")+1, cellDisplay.lastIndexOf("</td>"));

			column.setCellDisplay(cellDisplay);
			cellDisplay=null;

		}

        this.getHtmlBuilder().append(column.getCellDisplay());

        if(column.isLastColumn()){
        	getRowBuilder().rowEnd();
        }
	}

	@Override
	protected void afterBodyInternal(TableModel model) {

		getTableBuilder().getHtmlBuilder().tableEnd(2);
		buildEmptyRows(model);
//		根据ui加入table标签以及样式到列表数据body ---- end

	}


	/**
	 *
	     * Function:根据UI原型创建表格头部分
	     * @param
	     * @return
	     * Create author:yixianjian
	     * Create on:2008-5-14
	 */
	private void buildTableHead(TableModel model){
//		根据ui加入table标签以及样式到hearder----- begin
		String headTableClass = model.getPreferences().getPreference("table.headtable.style");
		String headTableWidth = model.getTableHandler().getTable().getWidth();
		String headTableBorder = model.getTableHandler().getTable().getBorder();
		getTableBuilder().getHtmlBuilder().table(2).width(headTableWidth).border(headTableBorder).styleClass(headTableClass).cellPadding("0").cellSpacing("0").close();
		getTableBuilder().headerRow();
		getTableBuilder().getHtmlBuilder().tableEnd(2);
	}
	private void buildTableDataBody(){
//		根据ui加入table标签以及样式到列表数据body ---- begin
		Table table = getTableModel().getTableHandler().getTable();
		String width = table.getWidth();
		String id = table.getTableId();
		String styleClass = table.getStyleClass();
		String cellPading = table.getCellpadding();
		String cellSpacing = table.getCellspacing();
		getTableBuilder().getHtmlBuilder().table(2).id(id+"_table").width(width).styleClass(styleClass).cellPadding(cellPading).cellSpacing(cellSpacing).close();
	}
	/**
     * Function:添加空行
     * Create author:yixianjian
     * Create on:2008-4-15
	 */
	private void buildEmptyRows(TableModel model){
		int totalRowsOfCurrentPage = model.getCollectionOfPageBeans().size();
		int dispalysRows = model.getTableHandler().getTable().getRowsDisplayed();
		int columnTotal = this.getTableModel().getColumnHandler().getColumns().size();

		String columnStyle = null;
		String columnWidth = null;
		Column temp = null;
		getHtmlBuilder().table(1).cellPadding("0").cellSpacing("0").border("0").close();

			for(int i = dispalysRows - totalRowsOfCurrentPage; i > 0; i--){

				if(totalRowsOfCurrentPage==0){
					getHtmlBuilder().tr(2);
				}else {
					getHtmlBuilder().tr(2).styleClass("grid");
				}
				if(totalRowsOfCurrentPage==0 && i-1==dispalysRows/2) {

					getHtmlBuilder().close();
						getHtmlBuilder().td(3).width(columnWidth).close().append("<h4>&nbsp;</h4>").tdEnd();

				} else {

					getHtmlBuilder().close();
			    getHtmlBuilder().td(3).width(columnWidth).close().append("&nbsp;").tdEnd();
				}
				getHtmlBuilder().trEnd(2);
			}

		getHtmlBuilder().tableEnd(1);
	}


}

